﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PsychveyApp1.classes;
using System.Collections;




namespace PsychveyApp1.classes
{

    public class MultipleChoiceQuestionPanel : System.Web.UI.WebControls.Panel
    {

        public TextBox tbxQuestion;
        public TextBox tbxAnswer;
        public RadioButtonList rblQuestionRadioButtonList;
        public Button btnQuestionButton;
        public Button btnAddAnswer;
        public Button btnRemoveAnswer;
        public Button btnEditAnswer;
        public Button btnDeleteQuestion;
        public Button btnSaveAnswer;
        public Button btnClear;
        public bool currentEditing = false;
        public DropDownList dropDownListMC;

        public ArrayList answerTextBoxArray;
        public ArrayList removeAnswerCheckboxArray;

        public Table tableMain;

        public Survey currentSurvey;
        public UserAccount currentUser;
        public Question currentQuestion;

        public ArrayList listOfAnswers;
        public ArrayList listOfQuestions;
        public int totalNoOfQuestions;

        // Default public constructor 
        public MultipleChoiceQuestionPanel(UserAccount user, Survey survey, Question question, ArrayList listOfAnswers,int totalNoQuestions, ArrayList listOfQuestions)
            : base()
        {
            // pass value from outside
            currentUser = user;
            currentSurvey = survey;
            currentQuestion = question;
            this.listOfAnswers = listOfAnswers;
            this.totalNoOfQuestions = totalNoQuestions;
            this.listOfQuestions = listOfQuestions;

            tableMain = new Table();
            
            //--------------------------
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            Label label = new Label();
            cell.ColumnSpan = 2;
            label.Text = "Question No";
            cell.Controls.Add(label);
            row.Cells.Add(cell);

            dropDownListMC = new DropDownList();
            for (int i = 0; i < this.totalNoOfQuestions; i++)
            {
                dropDownListMC.Items.Add(Convert.ToString(i+1));
            }
            dropDownListMC.SelectedValue = question.questionOrder;
            dropDownListMC.AutoPostBack = true;
            cell.Controls.Add(dropDownListMC); 
            //dropdownList.ID = "ddlQuestionNo";
            //dropdownList.Items.Insert(0, new ListItem("QNo", "QNo"));
            cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
            row.Cells.Add(cell);

            //cell = new TableCell();
            //btnEditAnswer = new Button();
            //btnEditAnswer.Text = "Edit Question";
            //cell.Controls.Add(btnEditAnswer);
            //cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
            //row.Cells.Add(cell);


            cell = new TableCell();
            btnDeleteQuestion = new Button();
            btnDeleteQuestion.Text = "Delete Question";
            btnDeleteQuestion.CausesValidation = false;
            cell.Controls.Add(btnDeleteQuestion);
            row.Cells.Add(cell);
            cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
            tableMain.Rows.Add(row);

            //-----------------------
            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.Text = "Question Type: " + question.typeOfQuestion;
            cell.ColumnSpan = 2;
            cell.Controls.Add(label);
            row.Cells.Add(cell);
            tableMain.Rows.Add(row);
            //-----------------------

            row = new TableRow();
            cell = new TableCell();
            cell.ColumnSpan = 4;
            tbxQuestion = new TextBox();
            tbxQuestion.Text = question.questionsAsked.ToString();
            tbxQuestion.Width = 500;
            cell.Controls.Add(tbxQuestion);
            row.Cells.Add(cell);
            tableMain.Rows.Add(row);

         

            //--------------------------
            row = new TableRow();
            cell = new TableCell();
            label = new Label();
            label.Text = "Answers";
            cell.ColumnSpan = 2;
            cell.Controls.Add(label);
            row.Cells.Add(cell);

            cell = new TableCell();
            btnAddAnswer = new Button();
            btnAddAnswer.Text = "Add Answer";
            btnAddAnswer.CausesValidation = false;
            //btnAddAnswer.PostBackUrl = "~/CreateSurvey2.aspx"; 
            cell.ColumnSpan = 1;
            // btnAddAnswer.ID = "btnAddAnswer";
            cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right; //alignment of cell
            cell.Controls.Add(btnAddAnswer);
            row.Cells.Add(cell);
           
            cell = new TableCell();
            cell.ColumnSpan = 1;
            btnRemoveAnswer = new Button();
            btnRemoveAnswer.Text = "Remove Selected Answer";
            btnRemoveAnswer.CausesValidation = false;
            cell.Controls.Add(btnRemoveAnswer);
            row.Cells.Add(cell);
            cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
            tableMain.Rows.Add(row);
            //--------------------------

            

            //--------------------------
            this.answerTextBoxArray = new ArrayList();
            this.removeAnswerCheckboxArray = new ArrayList();
            for (int i = 0; i < listOfAnswers.Count; i++)
            {
                row = new TableRow();
                cell = new TableCell();
                tbxAnswer = new TextBox();
                tbxAnswer.Text = ((Answer)this.listOfAnswers[i]).answerText;
                tbxAnswer.Width = 400;
                this.answerTextBoxArray.Add(tbxAnswer);
                cell.ColumnSpan = 3;
                cell.Controls.Add(tbxAnswer);
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.ColumnSpan = 1;
                CheckBox cbxAnswerCheckbox = new CheckBox();
                this.removeAnswerCheckboxArray.Add(cbxAnswerCheckbox);
                cell.Controls.Add(cbxAnswerCheckbox);
                row.Cells.Add(cell);
                cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                tableMain.Rows.Add(row);
            }

            this.Controls.Add(tableMain);


            //-----second table ------------
            Table secondTable = new Table();
            row = new TableRow();
            cell = new TableCell();
            btnSaveAnswer = new Button();
            btnSaveAnswer.Text = "Save";
            btnSaveAnswer.CausesValidation = false;
            cell.Controls.Add(btnSaveAnswer);
            row.Cells.Add(cell);

            //cell = new TableCell();
            btnClear = new Button();
            btnClear.Text = "Clear";
            btnClear.CausesValidation = false;
            cell.Controls.Add(btnClear);
            row.Cells.Add(cell);
            secondTable.Rows.Add(row);

            row = new TableRow();
            cell = new TableCell();
            cell.ColumnSpan = 2;
            Image breakLine = new Image();
            breakLine.ImageUrl = ("~/images/breakline.png");
            cell.Controls.Add(breakLine);
            row.Controls.Add(cell);
            secondTable.Rows.Add(row);

            this.Controls.Add(secondTable);

            //currentQuestion = new Question();
            //currentQuestion.questionID = currentUser.username + "__" + currentSurvey.title + "__" + currentQuestion.questionsAsked;
            //currentQuestion.typeOfQuestion = "";
            //currentQuestion.surveyID = currentUser.userID + "__" + currentSurvey.title;
            //currentQuestion.questionsAsked = "";
            //currentQuestion.userID = currentUser.userID;
            //DBManager.InsertQuestion(currentQuestion, currentSurvey, currentUser);
        }

        public void AddAnswer(object sender, EventArgs e)
        {
            
            DBManager.UpdateQuestion(currentUser.userID, currentSurvey.surveyID,
                    currentQuestion.questionID, tbxQuestion.Text);
            for (int i = 0; i < this.listOfAnswers.Count; i++)
            {    
                DBManager.UpdateAnswer(currentSurvey.surveyID, currentUser.userID, currentQuestion.questionID,
               ((Answer)this.listOfAnswers[i]).answerID, ((TextBox)this.answerTextBoxArray[i]).Text);
            }
           
            Answer answer = new Answer();
            answer.answerText = "";
            answer.questionID = currentQuestion.questionID;
            answer.userID = currentUser.userID;
            answer.surveyID = currentSurvey.surveyID;

            CurrentMaxIDNumber currentMaxAnswerID = DBManager.GetCurrentMaxIDNumber(this.currentUser.userID);
            currentMaxAnswerID.currentMaxAnswerID++;
            answer.answerID = this.currentUser.username + "__ANS" + currentMaxAnswerID.currentMaxAnswerID;
            DBManager.UpdateCurrentMaxIDNumber(currentMaxAnswerID);
            DBManager.InsertAnswer(answer, currentQuestion, currentSurvey, currentUser);

            
            // response redirect from a non web form class
           HttpContext.Current.Response.Redirect("CreateSurvey2.aspx");
           
            //((TextBox)this.answerTextBoxArray[0]).Text = "abc";
        }

        public void SaveAnswer(object sender, EventArgs e)
        {
            for (int i = 0; i < this.listOfAnswers.Count; i++)
            {
                DBManager.UpdateQuestion(currentUser.userID, currentSurvey.surveyID,
                    currentQuestion.questionID, tbxQuestion.Text);
                DBManager.UpdateAnswer(currentSurvey.surveyID, currentUser.userID, currentQuestion.questionID,
                    ((Answer)this.listOfAnswers[i]).answerID, ((TextBox)this.answerTextBoxArray[i]).Text);
            }
        }

        public void RemoveQuestion(object sender, EventArgs e)
        {
            for (int i = 0; i < this.listOfAnswers.Count; i++)
            {
                DBManager.DeleteAnswer(((Answer)this.listOfAnswers[i]).answerID, currentQuestion.questionID, currentSurvey.surveyID, currentUser.userID);
                //tbxQuestion.Text = ((Answer)this.listOfQuestions[i]).answerText;
                
            }
            DBManager.DeleteQuestion(currentUser.userID, currentSurvey.surveyID,
                   currentQuestion.questionID);
            DBManager.DeleteUserAnswers(currentUser.userID, currentSurvey.surveyID,
                  currentQuestion.questionID);
            int x = Convert.ToInt32(currentQuestion.questionOrder);
            for (int i = 0 ; i < this.totalNoOfQuestions ; i++)
            {
             
                  if ((Convert.ToInt32(((Question)listOfQuestions[i]).questionOrder) > x))
                  {
                      DBManager.UpdateQuestionOrder(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[i]).questionID, Convert.ToString(Convert.ToInt32(((Question)listOfQuestions[i]).questionOrder) - 1));
                  }
             }
            HttpContext.Current.Response.Redirect("CreateSurvey2.aspx");
          
        }

        public void RemoveAnswer(object sender, EventArgs e)
        {
            for (int c = 0; c < this.removeAnswerCheckboxArray.Count; c++)
            {
                if (((CheckBox)this.removeAnswerCheckboxArray[c]).Checked == true)
                {
                    Answer answer = (Answer)this.listOfAnswers[c];
                    DBManager.DeleteAnswer(((Answer)this.listOfAnswers[c]).answerID, currentQuestion.questionID, currentSurvey.surveyID, currentUser.userID);
                   
                }

            }

            HttpContext.Current.Response.Redirect("CreateSurvey2.aspx");
        }

      public void ClearForm(object sender, EventArgs e)
      {
          for (int i = 0; i < this.listOfAnswers.Count; i++)
          {
              tbxQuestion.Text = "";
              ((TextBox)this.answerTextBoxArray[i]).Text = "";
          }
      }

      public void UpdateQuestionOrder(object sender, EventArgs e)
      {
          int x = Convert.ToInt32(currentQuestion.questionOrder);
          int y = Convert.ToInt32(dropDownListMC.SelectedItem.Text);
          if (x > y)
          {
              for (int r = 0; r < this.listOfQuestions.Count; r++)
              {
                  if ((Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) < x) && ((Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) >= y)))
                  {
                      DBManager.UpdateQuestionOrder(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[r]).questionID, Convert.ToString(Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) + 1));
                  }
                  
              }
          }
          else if (x < y)
          {
              for (int r = 0; r < this.listOfQuestions.Count; r++)
              {
                  if ((Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) > x) && ((Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) <= y)))
                  {
                      DBManager.UpdateQuestionOrder(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[r]).questionID, Convert.ToString(Convert.ToInt32(((Question)listOfQuestions[r]).questionOrder) - 1));
                  }
              }
          }
          
          DBManager.UpdateQuestionOrder(currentUser.userID, currentSurvey.surveyID, currentQuestion.questionID, dropDownListMC.SelectedItem.Text);
          
          HttpContext.Current.Response.Redirect("CreateSurvey2.aspx");
      }
   }
 }
